Using Tensorflow 2.0 we call keras as a submodule of tensorflow. Below we create a sine wave and see how a dense layer network handles the prediction.


In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# Generate a sample sin wave
X = np.array([np.linspace(0,100,1000)])
y = np.sin(X)

# Plot the sample sin wave
plt.plot(y[0])
plt.show()

# Define a model to fit the above data
model = tf.keras.Sequential([
    tf.keras.layers.Dropout(rate=0.2, input_shape=X.shape[1:]),
    tf.keras.layers.Dense(units=64, activation='sigmoid'),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Dense(units=1000, activation='sigmoid')
])

# Compile the model
model.compile(loss='mse',
            optimizer='adam',
            metrics=['accuracy'])

# Fit the model
model.fit(X,y, epochs=10)

# Get the prediction
y_hat = model.predict(X)

# Plot the results
plt.plot(y_hat[0])
plt.show()


Train on 1 samples
Epoch 1/10
1/1 [==============================] - 0s 435ms/sample - loss: 0.7579 - accuracy: 0.0000e+00
Epoch 2/10
1/1 [==============================] - 0s 5ms/sample - loss: 0.7428 - accuracy: 0.0000e+00
Epoch 3/10
1/1 [==============================] - 0s 5ms/sample - loss: 0.7390 - accuracy: 0.0000e+00
Epoch 4/10
1/1 [==============================] - 0s 5ms/sample - loss: 0.7334 - accuracy: 0.0000e+00
Epoch 5/10
1/1 [==============================] - 0s 8ms/sample - loss: 0.7210 - accuracy: 0.0000e+00
Epoch 6/10
1/1 [==============================] - 0s 6ms/sample - loss: 0.7095 - accuracy: 0.0000e+00
Epoch 7/10
1/1 [==============================] - 0s 14ms/sample - loss: 0.7016 - accuracy: 0.0000e+00
Epoch 8/10
1/1 [==============================] - 0s 9ms/sample - loss: 0.6923 - accuracy: 0.0000e+00
Epoch 9/10
1/1 [==============================] - 0s 7ms/sample - loss: 0.6919 - accuracy: 0.0000e+00
Epoch 10/10
1/1 [==============================] - 0s 4ms/sample - loss: 0.6592 - accuracy: 0.0000e+00

Check the prediction after 1000 epochs


In [4]:
# Fit the model
model.fit(X,y, epochs=1000, verbose=0)

# Get the prediction
y_hat = model.predict(X)

# Plot the results
plt.plot(y_hat[0])
plt.show()



In [ ]: